<template>
  <div style="width: 100%; height: 100%;">
    <div id="map" class="map" style="width: 100%; height: 100%;"></div>
  </div>
</template>

<script>
import '@/style/ol.css';
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import Feature from 'ol/Feature.js';
import TileLayer from 'ol/layer/Tile.js';
import XYZ from 'ol/source/XYZ.js';
import LayerVector from 'ol/layer/Vector';
import SourceVector from 'ol/source/Vector.js';
import {Fill, Stroke, Style} from 'ol/style.js';

import Select from 'ol/interaction/Select.js';
import {altKeyOnly, click, pointerMove} from 'ol/events/condition.js';

import GeoJSON from "ol/format/GeoJSON";
import {transform} from "ol/proj";
import Tianditu from "@/lang/tianditu";

/**
 * 这是一个常见的需求，鼠标悬浮的时候，对应的区域需要增强展示效果
 */
export default {
    components: {},
    data() {
        return {
            map: null
        };
    },
    mounted() {
        this.initMap();
    },
    methods: {
        initMap() {
            // 空白图层
            let layerVector = new LayerVector({
                style: Tianditu.defaultStyle
            });

            let map = new Map({
                view: new View({
                    // 指定大地坐标系
                    projection: 'EPSG:4326',
                    center: [102.0, 0.5],
                    zoom: 7
                }),
                layers: [
                    // 底图
                    new TileLayer({
                        source: new XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}'})
                    }),
                    layerVector
                ],
                target: 'map',
            });

            // 一个点击事件
            map.on('click', function (evt) {
                // 鼠标点击的坐标
                let coordinate = evt.coordinate;
                console.log('click: ' + coordinate);

                map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
                    Layers.succeed('功能建设中！');
                });
            });

            this.map = map;


            const selected = new Style({
                fill: new Fill({
                    color: '#eeeeee',
                }),
                stroke: new Stroke({
                    color: 'rgba(255, 255, 255, 0.7)',
                    width: 1,
                }),
            });

            function selectStyle(feature) {
                const color = feature.get('COLOR') || '#eeeeee';
                selected.getFill().setColor(color);
                return selected;
            }

            // 选择交互
            // select interaction working on "pointermove"
            const selectPointerMove = new Select({
                condition: pointerMove,
                style: selectStyle,
            });

            // 增加交互操作
            map.addInteraction(selectPointerMove);

            // 测试用 geojson
            let geojson = {
                "type": "FeatureCollection",
                "features": [
                    {
                        "type": "Feature",
                        "geometry": {
                            "type": "Polygon",
                            "coordinates": [[[100.89038,1.74145], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0], [100.89038,1.74145]]]
                        },
                        "properties": {
                            "prop0": "value0",
                            "prop1": 0.0
                        }
                    }
                ]
            };

            layerVector.setSource(new SourceVector({
                features: new GeoJSON({featureProjection: 'EPSG:4326'}).readFeatures(geojson)
            }));
        }
    },


};
</script>
